class Node {
    constructor(element){
        this.element = element;
        this.next = null;
    }
}
class CircleList{
    constructor(){
        this.head = null ; //头元素
        this.count = 0;//人数
    }
    //查找指定的元素  循环迭代
    getElementAt(index){
        let current = this.head;
        for(let i = 0 ; i < index ; i++){
            current=current.next
        }
        return current
    }
    //末尾添加
    push(element){
        let node = new Node(element)
        if(this.head === null){
            this.head = node;
            node.next = this.head;
        }else{
            //尾部
            let last = this.getElementAt(this.count - 1)

            last.next = node;
            node.next = this.head;
        }
        this.count++;
    }
    //删除
    remove(){

    }
    //n 人数 k 报数
    ysfCircle(n,k){
        for(let i = 1 ; i <= n ; i++){
            this.push(i)
        }
        console.log(this.count)
        let heler = this.head;//记录上一个
        let current = this.head;//要删除的元素

        while(heler.next != this.head){//helper 移动到最后一个
            heler = heler.next
        }
        //此时helper 的位置再thia.head上一个
        while(this.count > 15){
            for(let i = 1 ; i < k ; i++){
                heler = heler.next//30 -> 8
                current=current.next;//1 -> 9
            }
            console.log('要淘汰的人',current)
            //9的一个复制给8的下一个也就是10
            heler.next= current.next;
            //更新当前 10
            current = current.next;
            //人数减去
            this.count--;
        }
    }

}
let C = new CircleList()

C.ysfCircle(30,9)

console.log(C)